<template>
  <div class="components">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <template #title>
          <div class="cmt_wrap">
            <van-image
              round
              width="30px"
              height="30px"
              fit="cover"
              :src="item.aut_photo"
            />
            <div class="my_cmt">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="date">
                {{ item.pubdate | relvTime }}
                <van-button size="mini" @click="showReply(item)"
                  >回复({{ item.reply_count }})</van-button
                >
              </div>
            </div>
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>
    <div  class="cmt-input-wrap">
      <van-field
        v-model="sms"
        center
        @keyup.enter="addComment"
        clearable
        placeholder="请输入评论"
      >
        <template #button>
          <van-button size="small" type="info" @click="addComment"
            >发布</van-button
          >
        </template>
      </van-field>
    </div>

    <van-action-sheet v-model="sheetShow" v-if="sheetShow" title="评论内容">
      <reply  :currentItem="currentItem" />
    </van-action-sheet>
  </div>
</template>

<script>
import { cmtListAPI, conmentAPI } from "@/api";
import reply from "./reply.vue";
export default {
  components: { reply },
  props: ["id"],
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      sms: "",
      offset: undefined,
      sheetShow: false,
      currentItem: {},
    };
  },

  methods: {
    showReply(item) {
      this.sheetShow = true;
      this.currentItem = item;
    },
    async onLoad() {
      // 异步更新数据
      //   // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      //   setTimeout(() => {
      //     for (let i = 0; i < 10; i++) {
      //       this.list.push(this.list.length + 1);
      //     }

      //     // 加载状态结束
      //     this.loading = false;

      //     // 数据全部加载完成
      //     if (this.list.length >= 40) {
      //       this.finished = true;
      //     }
      //   }, 1000);
      let res = await cmtListAPI({
        type: "a",
        source: this.id,
        offset: this.offset,
      });
      //   console.log(res);
      this.list.push(...res.data.data.results);
      this.offset = res.data.data.last_id;
      this.loading = false;
      if (res.data.data.end_id == res.data.data.last_id) {
        this.finished = true;
      }
    },
    async addComment() {
      if (this.sms == "") {
        this.$toast("输入内容");
        return;
      }
      let res = await conmentAPI({
        target: String(this.id),
        content: this.sms,
        art_id: String(this.id),
      });
      // console.log(res);
      this.list.unshift(res.data.data.new_obj);
      this.sms = "";
    },
  },
};
</script>

<style lang="less" scoped>
.components {
  margin-top: 20px;
  ::v-deep .cmt_wrap {
    display: flex;
    .my_cmt {
      margin-left: 15px;
      flex: 1;
      .name {
        color: #466b9d;
      }
      .content {
        margin: 8px 0;
      }
      .date {
        height: 20px;
        display: flex;
        align-items: center;
        color: gray;
      }
    }
  }
  .cmt-input-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
}
</style>